import { getToken, setToken, removeToken } from '@/utils/auth'
import { loginApi } from '@/apis/user'

export default {
  namespaced: true,
  state: {
    token: getToken() || ''
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      setToken(token)
    },
    clearUserInfo(state) {
      state.token = ''
      removeToken()
    }
  },
  actions: {
    async login(context, userInfo) {
      const res = await loginApi(userInfo)
      context.commit('setToken', res.data.token)
    }
  }
}
